app.controller('UserController', function ($scope, UserControl) {
    $scope.userControl = new UserControl();
});

app.factory('UserControl', function ($http, $sce) {
    var UserControl = function () {
        this.urlConnect = HTTPNAME + "/index.php/connection";
        this.urlDisconnect = HTTPNAME + "/index.php/disconnect";
        this.user;
        this.email = "";
        this.password = "";
        this.error = "";
        this.connectionClass = "";
        this.disconnectClass = "";
        this.contentText = "";
        this.textClass = "";
        $http({
            url: this.urlConnect,
            data: '',
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        }).success(function (data) {
                this.createViewSuccess(data);
            }.bind(this))
            .error(function (data) {
                this.createViewError(data);
            }.bind(this));
    };
    UserControl.prototype.createViewSuccess = function (data) {
        this.connectionClass = "hide";
        this.disconnectClass = "";
        this.user = data;
        this.contentText = $sce.trustAsHtml('Logged in ' + this.user.firstname + ' ' + this.user.lastname + '<span class="transform90">&lt;</span>');
        this.textClass = 'text';
        this.mouseOverFunction();
    };
    UserControl.prototype.connect = function () {
        $http({
            url: this.urlConnect,
            data: 'email=' + this.email + '&password=' + this.password,
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        }).success(function (data) {
                $.gritter.add({
                    title: 'Login',
                    text: 'You are logged'
                });
                this.createViewSuccess(data);
            }.bind(this))
            .error(function (data) {
                this.createViewError(data);
            }.bind(this));
    };
    UserControl.prototype.disconnect = function () {
        $http.get(this.urlDisconnect).success(function (data) {
            $.gritter.add({
                title: 'Logout',
                text: 'You are unlogged'
            });
            this.createViewError(data);
        }.bind(this));
    };
    UserControl.prototype.createViewError = function (data) {
        this.error = $sce.trustAsHtml("");
        if (data != undefined && data != '') {
            this.error = $sce.trustAsHtml(data.error);
        }
        this.connectionClass = "";
        this.disconnectClass = "hide";
        this.contentText = $sce.trustAsHtml('Connection <span class="transform90">&lt;</span>');
        this.textClass = 'text';
        this.mouseOverFunction();
    };
    UserControl.prototype.mouseOverFunction = function () {
        $(function () {

            $('#user').unbind('mouseover').unbind('mouseout');
            $('#user').stop().mouseover(function () {
                $('.connection', this).dequeue();
                $('#user .connection').height('auto');
                $('#user .connection').width('auto');
                $('#user .text span.transform90').html('&gt;');
                $('.connection', this).stop().show(500);
            }).stop().mouseout(function () {
                //$('.connection', this).dequeue();
                $('#user .connection').height('auto');
                $('#user .connection').width('auto');
                $('#user .text span.transform90').delay(1000).html('&lt;');
                $('.connection', this).stop().delay(1000).hide(500);
                setTimeout(function () {
                    alert(jojo);
                }, 5000);
            });
        });

    };
    return UserControl;
});